package com.demo.controller;

import com.demo.common.permission.PermissionCheck;
import com.demo.common.permission.PermissionKey;
import com.demo.common.permission.login.RequestService;
import com.demo.common.vo.Result;
import com.demo.entity.SysUser;
import com.demo.entity.vo.IdRequest;
import com.demo.entity.vo.SysUserSearchReq;
import com.demo.entity.vo.SysUserUpdatePwdReq;
import com.demo.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RequestService requestService;

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @PostMapping("/add")
    @PermissionCheck({PermissionKey.VIEW_USER_MANAGER,PermissionKey.VIEW_ROLE_MANAGER})
    public Result add(@RequestBody SysUser sysUser){
        if(StringUtils.isBlank(sysUser.getMobile())){
            return Result.error("用户手机号不能为空");
        }
        if(sysUser.getRoleId()==null){
            return Result.error("用户角色不能为空");
        }
        sysUserService.register(sysUser);
        return Result.success();
    }

    /**
     * 删除用户
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result delete(@RequestBody IdRequest request){
        sysUserService.removeById(request.getId());
        return Result.success();
    }

    /**
     * 更新用户信息（角色，头像）
     * @param sysUser
     * @return
     */
    @PostMapping("/update")
    @PermissionCheck({PermissionKey.VIEW_USER_MANAGER,PermissionKey.VIEW_ROLE_MANAGER})
    public Result update(@RequestBody SysUser sysUser){
        if(sysUser.getId()==null){
            return Result.error("更新用户id不能为空");
        }
        if(sysUser.getRoleId()==null){
            return Result.error("更新用户角色不能为空");
        }
        sysUserService.updateUser(sysUser);
        return Result.success();
    }

    /**
     * 用户更新密码
     * @param request
     * @return
     */
    @PostMapping("/update-pwd")
    public Result updatePwd(@RequestBody @Valid SysUserUpdatePwdReq request){
        sysUserService.updatePwd(requestService.getUserId(), request.getOldPwd(),request.getNewPwd());
        return Result.success();
    }

    /**
     * 冻结用户
     * @param request
     * @return
     */
    @PostMapping("/freeze")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result freezeUser(@RequestBody @Valid IdRequest request){
        sysUserService.freezeUser(request.getId());
        return Result.success();
    }

    /**
     * 解冻用户
     * @param request
     * @return
     */
    @PostMapping("/unfreeze")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result unfreezeUser(@RequestBody @Valid IdRequest request){
        sysUserService.unfreezeUser(request.getId());
        return Result.success();
    }

    /**
     * 重置密码
     * @param request
     * @return
     */
    @PostMapping("/rest-pwd")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result resetPwd(@RequestBody @Valid IdRequest request){
        sysUserService.resetPwd(request.getId());
        return Result.success();
    }

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("/list")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result list(@RequestBody SysUserSearchReq request){
        return Result.success(sysUserService.userList(request));
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @PermissionCheck(PermissionKey.VIEW_USER_MANAGER)
    public Result list(@RequestParam Integer id){
        return Result.success(sysUserService.getById(id));
    }

}
